#include "mutex.hpp"
using namespace my_mutex;
int ticket = 1000;
Mutex mtx;
void *fun(void *p)
{
    string name = static_cast<char *>(p);
    while (true)
    {
        //mtx.Lock();
        LockGuard lock(mtx);
        if (ticket > 0)
        {
            usleep(1000);
            cout << name << ':' << ticket << endl;
            ticket--;
            //mtx.Unlock();
            usleep(200);
        }
        else
        {
            //mtx.Unlock();
            break;
        }
    }
    return nullptr;
}
int main()
{
    pthread_t tid1, tid2, tid3;
    //Mutex* mtx = new Mutex();
    pthread_create(&tid1, nullptr, fun, (void *)"thread-1");
    pthread_create(&tid2, nullptr, fun, (void *)"thread-2");
    pthread_create(&tid3, nullptr, fun, (void *)"thread-3");
    pthread_join(tid1, nullptr);
    pthread_join(tid2, nullptr);
    pthread_join(tid3, nullptr);
    return 0;
}